package register;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import db_tool.TimeFormat;
import org.apache.struts2.ServletActionContext;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.util.MD5Util;
import tree_util.UpLoadPicture;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;


public class Register extends ActionSupport{
	private String flag;		//区分操作
	 
	private String UserName;
	private String PassWord;
	private String Telephone;
	private String Email;
	private String Company;
	private String Address;
	
	private static String PATH = "";					//照片在服务器上的路径("|"隔开)
	private List<File> Photo;					//文件引用
	private List<String> PhotoFileName;			//文件名字
	private List<String> PhotoContentType;		//文件类型
	private String isNull;
	
	private String deletePhoto;
	private String NodeName;
	private PrintWriter writer = null;
	private DBConnection db_connection = null;
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getUserName() {
		return UserName;
	}
	public void setUserName(String UserName) {
		this.UserName = UserName;
	}
	public String getPassWord() {
		return PassWord;
	}
	public void setPassWord(String PassWord) {
	    this.PassWord=PassWord;
	}
	public String getTelephone() {
		return Telephone;
	}
	public void setTelephone(String Telephone) {
		this.Telephone = Telephone;
	}
	public String getEmail() {
		return Email;
	}
	public void setEmail(String Email) {
		this.Email = Email;
	}
	public String getCompany() {
		return Company;
	}

	public void setCompany(String company) {
		Company = company;
	}

	public String getAddress() {
		return Address;
	}

	public void setAddress(String address) {
		Address = address;
	}

	public List<File> getPhoto() {
		return Photo;
	}

	public void setPhoto(List<File> photo) {
		Photo = photo;
	}

	public List<String> getPhotoFileName() {
		return PhotoFileName;
	}

	public void setPhotoFileName(List<String> photoFileName) {
		PhotoFileName = photoFileName;
	}

	public List<String> getPhotoContentType() {
		return PhotoContentType;
	}

	public void setPhotoContentType(List<String> photoContentType) {
		PhotoContentType = photoContentType;
	}
	
	public String getDeletePhoto(){
		return deletePhoto;
	}
	
	public void setDeletePhoto(String deletePhoto){
		this.deletePhoto=deletePhoto;
	}
	
	public String getNodeName(){
		return NodeName;
	}
	
	public void setNodeName(String NodeName){
		this.NodeName=NodeName;
	}
	
	public String getIsNull(){
		return isNull;
	}
	
	public void setIsNull(String isNull){
		this.isNull=isNull;
	}
	
	public String getAllPhoto() throws Exception{
		writer = ServletActionContext.getResponse().getWriter();
		PATH = UpLoadPicture.getAllPhoto(NodeName);
		System.out.println("......................................"+NodeName+"             "+PATH);
		writer.write(PATH);
		return null;
	}
	
	//执行插入操作，根据得到的改变的行数确定是否成功
	public String insert() throws Exception{
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		writer = ServletActionContext.getResponse().getWriter();
				db_connection = DBConnection.getInstance();
		String Id = "REG"+ TimeFormat.getNowTime();
		
		System.out.println(PhotoFileName);
		System.out.println("传过来了");
		System.out.println(isNull);
		PATH = UpLoadPicture.upload(Photo, PhotoFileName, Id);
		if(PATH.equals("photo_error")){			//照片出错
			writer.write("photo_error");
		}else{
			String sql="insert into RegisterAccount values(?,?,?,?,?,?,?,?,?)";
			PreparedStatement preparedStatement = null;
		try {
			preparedStatement = db_connection.getPreparedStatement(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			preparedStatement.setString(1, Id);
			preparedStatement.setString(2, UserName);
			preparedStatement.setString(3, MD5Util.getMD5(PassWord));
			preparedStatement.setString(4, Telephone);
			preparedStatement.setString(5, Email);
			preparedStatement.setString(6, "no");
			preparedStatement.setString(7, Company);
			preparedStatement.setString(8, PATH);
			preparedStatement.setString(9, Address);
			System.out.println(UserName);
			System.out.println(PassWord);
			System.out.println(PassWord);
			System.out.println(Telephone);
			System.out.println(Email);
			System.out.println(Company);
			System.out.println(Address);
			System.out.println(PATH);

			if(UserName.length()==0||PassWord.length()==0||Telephone.length()==0
					||Email.length()==0||Company.length()==0||Address.length()==0){
				writer.write("error");
				System.out.println("1");
			}
			else {
				System.out.println("1.5");
				preparedStatement.executeUpdate();
				System.out.println("2");
				System.out.println(sql);
				writer.write("success");
			}			
		} catch (SQLException e) {
			writer.write("error");
			System.out.println("--插入错误--");
			
			e.printStackTrace();
		}finally{
			try {
				db_connection.close(preparedStatement);
			} catch (SQLException e) {
				System.out.println("插入操作关闭失败");
				e.printStackTrace();
			}
			writer.flush(); writer.close();
		}
	  }
	  return SUCCESS;
	}

	public String sendEmail(){
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		String to = Email; // 收件人
		String from = "galigygy@163.com"; // 发件人
		String host = "smtp.163.com"; // smtp主机
		String subject = "激活成功"; // 邮件主题
		String content = "" // 邮件正文
				+"尊敬的"+UserName+":<br/>"
				+"您好！感谢您使用长江上游珍稀特有鱼类信息管理系统。恭喜您注册成功！<br/><br/>"
				+"网址：http://fish.yfi.ac.cn<br/><br/>"
				+"如有任何疑问或需要帮助，欢迎随时联系我们。<br/>"
				+"再次感谢您使用本系统，谢谢！<br/><br/>"
				+"长江水产研究所";
		// 获取系统属性对象
		Properties properties = System.getProperties();

		// 构造mail session
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		System.out.println("section1");
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("galigygy@163.com", "galigygy11111");
					}
				});
		System.out.println("section2");
		try {
			// 构造MimeMessage并设定基本的值，创建消息对象
			MimeMessage msg = new MimeMessage(session);
			// 设置消息内容
			msg.setFrom(new InternetAddress(from));
			// 把邮件地址映射到Internet地址上
			InternetAddress[] address = { new InternetAddress(to) };
			/**
			 * setRecipient（Message.RecipientType type, Address
			 * address），用于设置邮件的接收者。<br>
			 * 有两个参数，第一个参数是接收者的类型，第二个参数是接收者。<br>
			 * 接收者类型可以是Message.RecipientType .TO，Message
			 * .RecipientType.CC和Message.RecipientType.BCC，TO表示主要接收人，CC表示抄送人
			 * ，BCC表示秘密抄送人。接收者与发送者一样，通常使用InternetAddress的对象。
			 */
			msg.setRecipients(Message.RecipientType.TO, address);
			// 设置邮件的标题
			msg.setSubject(subject);
			// 构造Multipart
			Multipart mp = new MimeMultipart();

			// 向Multipart添加正文
			MimeBodyPart mbpContent = new MimeBodyPart();
			// 设置邮件内容(纯文本格式)
			// mbpContent.setText(content);
			// 设置邮件内容(HTML格式)
			mbpContent.setContent(content, "text/html;charset=utf-8");
			// 向MimeMessage添加（Multipart代表正文）
			mp.addBodyPart(mbpContent);
			// 向Multipart添加MimeMessage
			msg.setContent(mp);
			// 发送邮件
			Transport.send(msg);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

